package cqupt.spark_log.entity;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Set;

import cqupt.spark_log.fileio.FileReadMan;

public class StrategyTable {
	private HashMap<String, HashMap<String, HashMap<String, String[]>>> result = new HashMap<String, HashMap<String, HashMap<String, String[]>>>();
	private String[] heads = null;
	public StrategyTable(String path) {
		initTableInfo(path);
	}

	private void initTableInfo(String path) {
		FileReadMan fileReadMan = new FileReadMan(path, false);
		String headLine = null;
		if (fileReadMan.hasNext()) {
			headLine = fileReadMan.next();
		}
		heads = headLine.trim().split(",");
		while (fileReadMan.hasNext()) {
			String line = fileReadMan.next();
			String[] split = line.split(",");
			String jobStageTask = split[0];
			String[] jobStageTaskArray = jobStageTask.split(";");

			String taskkey = jobStageTaskArray[2];
			String jobStageKey = jobStageTaskArray[0] + ";" + jobStageTaskArray[1];

			HashMap<String, String[]> engyMap = new HashMap<String, String[]>();
			for (int i = 1; i < split.length; i++) {
				String executor = split[i];
				engyMap.put(heads[i], executor.split(";"));
			}
			if (result.containsKey(jobStageKey)) {
				HashMap<String, HashMap<String, String[]>> engyMaptemp = result.get(jobStageKey);
				engyMaptemp.put(taskkey, engyMap);
			} else {
				HashMap<String, HashMap<String, String[]>> taskMap = new HashMap<String, HashMap<String, String[]>>();
				taskMap.put(taskkey, engyMap);
				result.put(jobStageKey, taskMap);
			}
		}
	}
	//操作
	/**
	 * 获取节点列表
	 */
	public String[] getNodeList() {
		String[] result = new String[heads.length-1];
		System.arraycopy(heads, 1, result, 0, heads.length-1);
		Arrays.sort(result);
		return result;
	}
	/**
	 * 获取阶段列表
	 */
	public String[] getStageList() {
		Set<String> keySet = result.keySet();
		String[] result = keySet.toArray(new String[keySet.size()]);
		Arrays.sort(result);
		return result;
	}

	public HashMap<String, HashMap<String, String[]>> getStageInfoByStageId(String stageId) {
		return result.get(stageId);
	}
}
